<?php

/**
 * User: day
 * Date: 2018/08/07
 * Time: 23:04
 * Description:
 */

namespace App\Console\Commands\ThirdOrder;


use App\Services\ThirdAPI\GameApiService;
use App\Services\ThirdAPI\FGAPIService;
use App\Services\UserService;
use common\utils\RedisKeys;
use Illuminate\Support\Facades\Redis;


class FGOrderProcessor extends BaseOrderProcessor {
    protected $signature = self::PREFIX . 'fg {--type=}';
    protected $description = 'FG注单拉取';

    public $gameNames;
    public $fgService;
    // 拉取注单的区间长度，比如从 4:00 - 5:00 一个小时的长度，根据注单量调整
    public $timeInterval = 60 * 60;
    // 自动重拉更新锁 redis key
    public $lockRedisKey = RedisKeys::FG_LOCK_AUTOFIX;
    // 拉取注单更新时间 redis key
    public $updateRedisKey = RedisKeys::FG_UPDATE_TIME;
    // 当前最新的拉取注单更新时间
    public $latestUpdateKey;

    //  fish(捕猎),poker(棋牌),slot(老虎机),fruit(水果机)
    const FG_GAME_TYPE = ['fish', 'slot', 'fruit', 'poker'];

    /**
     * 初始化信息，子类需要重写这个方法
     */
    public function init($argv = null)
    {
        // 必须调用父类init，初始化公共信息
        parent::init();
        $this->fgService = new FGAPIService();
        // 5.第三方 partner id，名字，对应第三方表id
        $this->partnerName = "FG";
        $info = GameApiService::getApiPartnerInfoByName($this->partnerName);
        $this->partnerID = $info['id'];
        $this->orderType = self::ORDER_TYPE_EGAME;
        $this->reportOrderType = self::GAME_TYPE_EGAME;
        $this->gameNames = $this->fgService->getFGGameNames();
        $this->userInfos = [];
    }

    /**
     * 向第三方API抓取数据
     */
    public function catchData()
    {
        $startTime = OrderStrategy::getUpdateKey($this->lockRedisKey, $this->updateRedisKey, RedisKeys::FG_UPDATE_KEY . '*');
        $startTime = strtotime($startTime);
        // FG 会延迟10秒数据, 拉取 $this->timeInterval 的数据
        $this->latestUpdateKey = $endTime = min(time(), $startTime + $this->timeInterval) - 15;
        $data = $this->fgService->getFGOrders($startTime, $endTime);
        return $data;
    }

    /**
     * 处理数据
     * 不可以放复杂操作，因为拉取到的每个数据都会经过 load 加载
     */
    public function load($data)
    {
        //  fish(捕猎),poker(棋牌),slot(老虎机),fruit(水果机)
        if ( isset($data['gt']) && in_array($data['gt'], array_slice(self::FG_GAME_TYPE,1)) ) {
            $this->betTime = date('Y-m-d H:i:s', $data['time']); //只有这个结束时间
            $this->settleTime = $this->reportTime = $this->betTime;
            $this->betMoney = round(amount_format($data['all_bets'],true), 0);
            $this->prizeMoney = round(amount_format($data['all_wins'],true) , 0);
            $this->winLossMoney = round(amount_format($data['result'],true), 0);
            $this->apiUserName = $data['player_name'];
            $this->contributeMoney = intval(amount_format($data['jp_contri'],true));    //贡献  第三方返回数据为保留四位小数字符串，这边只保留到分，其余舍弃
            $this->bonusMoney = intval(amount_format($data['jackpot_bonus'],true));     //奖金  精度与贡献相同
            $this->validMoney = $this->betMoney;
        } else { //捕鱼 和其他游戏类型注单记录不同，做区别处理
            $data['gt'] = current(self::FG_GAME_TYPE);
            $this->betTime = date('Y-m-d H:i:s', $data['start_time']);
            $this->settleTime = $this->reportTime = date('Y-m-d H:i:s', $data['end_time']);
            $this->betMoney = round(amount_format($data['bullet_chips'],true) , 0);
            $this->prizeMoney = round(amount_format($data["fish_dead_chips"],true) , 0);
            $this->winLossMoney = round(amount_format($data['fish_dead_chips'] - $data['bullet_chips'],true) , 0);
            $this->apiUserName = $data['nickname'];
            $this->contributeMoney = intval(amount_format($data['jp_add'],true) );    //贡献
            $this->bonusMoney = intval(amount_format($data['trans_chips'],true) );     //奖金
            $this->validMoney = $this->betMoney;

            // "type":int 类型 1->场景捕鱼账单.2->jp 奖账单.6->买鱼注单.14->激光炮.15->boss赛功能
            // if($data['type'] == 2)  //去除jp派奖影响
            // {
            //     // trans_chips ??
            //     $this->prizeMoney = 0;
            //     $this->winLossMoney = 0;
            // }
        }
        $this->commonStatus = self::COMMON_STATUS_SETTLE;
        $this->updateKey = isset($data['start_time'])?date("Y-m-d H:i:s",$data['start_time']):date("Y-m-d H:i:s",$data['time']);
        $this->apiStatus = 1;
        $this->gameCode = $data['game_id'];
        $this->gameName = $this->gameNames[$data['game_id']];
        $this->betContent = "";
        $this->originData = json_encode($data);
        $this->orderNumber = $data["id"]."_".$data['gt'] ;

    }

    /*
     * 更新redis拉取注单ID
     */
    public function updateFlag($updateKey){
        Redis::connection()->set($this->updateRedisKey, date("Y-m-d H:i:s", $this->latestUpdateKey));
    }

    /**
     * 处理数据，每个子类需要根据第三方的单条数据内容来获取有用的信息
     */
    public function initFromData($data)
    {

    }

    /**
     * 修复数据内容，每个子类需要实现怎么修复数据内容
     */
    public function fixData($date)
    {

    }
}